<?php
Yii::import('application.components.Reference');
class BotolController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest)
            return;
        if (isset ($_POST) && !empty ($_POST)) {
            Yii::import('application.components.Gl');
            $gl = new Gl();
            $is_new = $_POST ['mode'] == 0;
            $botol_id = $_POST ['id'];
            $status = false;
            $msg = 'Botol berhasil disimpan.';
            Yii::import('application.components.U');
            $detils = CJSON::decode($_POST ['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $botol = $is_new ? new Botol : $this->loadModel($botol_id,
                    "Botol");
                $docref = $botol->doc_ref;
                if ($is_new) {
                    $ref = new Reference();
                    $docref = $ref->get_next_reference(BOTOL);
                }
                foreach ($_POST as $k => $v) {
                    if (is_angka($v))
                        $v = get_number($v);
                    $_POST ['Botol'] [$k] = $v;
                }
                $_POST['Botol']['doc_ref'] = $docref;
                $botol->attributes = $_POST ['Botol'];
                if (!$botol->save())
                    throw new Exception("Gagal menyimpan return botol. " . CHtml::errorSummary($botol));
                BotolDetil::model()->deleteAll('botol_id  = :botol_id',
                    array(':botol_id' => $botol_id));
                foreach ($detils as $detil) {
                    $botol_detil = new BotolDetil;
                    $_POST['BotolDetil']['jenis_botol_id'] = $detil['jenis_botol_id'];
                    $_POST['BotolDetil']['jml'] = get_number($detil['jml']);
                    $_POST['BotolDetil']['price'] = get_number($detil['price']);
                    $_POST['BotolDetil']['total'] = get_number($detil['total']);
                    $_POST['BotolDetil']['botol_id'] = $botol->botol_id;
                    $botol_detil->attributes = $_POST['BotolDetil'];
                    if (!$botol_detil->save())
                        throw new Exception("Gagal menyimpan botol detil. " . CHtml::errorSummary($botol_detil));
                }
                if ($is_new) {
                    $ref->save(BOTOL, $botol->botol_id, $docref);
                }
                $gl->add_gl(BOTOL, $botol->botol_id, $botol->tgl, $botol->doc_ref,
                    PIUTANGBOTOL, '', '', $botol->kas_keluar);
                $gl->add_gl(BOTOL, $botol->botol_id, $botol->tgl, $botol->doc_ref,
                    KASSEMENTARA, '', '', -$botol->kas_keluar);
                $gl->validate();
                $transaction->commit();
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg
            ));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Botol');
        if (isset ($_POST) && !empty ($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v))
                    $v = get_number($v);
                $_POST ['Botol'] [$k] = $v;
            }
            $msg = "Data gagal disimpan";
            $model->attributes = $_POST ['Botol'];
            if ($model->save()) {
                $status = true;
                $msg = "Data berhasil di simpan dengan id " . $model->botol_id;
            } else {
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array(
                    'success' => $status,
                    'msg' => $msg
                ));
                Yii::app()->end();
            } else {
                $this->redirect(array(
                    'view',
                    'id' => $model->botol_id
                ));
            }
        }
    }
    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $msg = 'Data berhasil dihapus.';
            $status = true;
            try {
                $this->loadModel($id, 'Botol')->delete();
            } catch (Exception $ex) {
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg
            ));
            Yii::app()->end();
        } else
            throw new CHttpException (400, Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
    public function actionIndex()
    {
        if (isset ($_POST ['limit'])) {
            $limit = $_POST ['limit'];
        } else {
            $limit = 20;
        }
        if (isset ($_POST ['start'])) {
            $start = $_POST ['start'];
        } else {
            $start = 0;
        }
        $criteria = new CDbCriteria ();
        if ((isset ($_POST ['mode']) && $_POST ['mode'] == 'grid') || (isset ($_POST ['limit']) && isset ($_POST ['start']))) {
            $criteria->limit = $limit;
            $criteria->offset = $start;
        }
        $model = Botol::model()->findAll($criteria);
        $total = Botol::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}